﻿/************************************
 * Module Name    : Message
 * Purpose        : Shows message
 * Chronological Development
 * raghunandan sharma
  ***********************************/
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using VAdvantage.Framework;
using VAdvantage.Common;
using VAdvantage.Utility;
using VAdvantage.Logging;
using System.Text;


namespace VAdvantage.Classes
{
    /// <summary>
    /// Shows message
    /// </summary>
    public class ShowMessage
    {
        /** Answer OK (0)       */
        public const int A_OK = 0;
        /** Answer Cancel (1)   */
        public const int A_CANCEL = 1;
        /** Answer Close (-1) - Default	*/
        public const int A_CLOSE = -1;
        /** Answer				*/
        private int m_returnCode = A_CLOSE;
        private static VLogger _log = VLogger.GetVLogger(typeof(ShowMessage).FullName);
        /**	Logger			*/
        /// <summary>
        /// Ask Question with question icon and (OK) (Cancel) buttons
        /// </summary>
        /// <param name="keyName">Keyword Name</param>
        /// <param name="isMsgText">if true returns Message Text, if false returns Message Tip
        ///     and if null then returns both message text and tip.</param>
        /// <param name="extraMsg">extra message to be displayed</param>
        /// <returns>Bool type-true if user clicks OK button else false</returns>
        /// <author>Veena Pandey</author>
        public static bool Ask(string keyName, bool? isMsgText, string extraMsg)
        {
            return Ask(keyName, isMsgText, extraMsg, "");
        }

        public static Message AskMessage (string keyName, bool? isMsgText, string extraMsg)
        {
            return AskMessage(keyName, isMsgText, extraMsg, "");
        }

        public static Message AskMessage(string keyName, bool? isMsgText, string extraMsg, string header)
        {
            StringBuilder content = new StringBuilder();
            // if user has given a key
            if (keyName != null && !keyName.Equals(""))
            {
                // get key's value
                content.Append(Msg.GetMsg(Envs.GetContext(), keyName));
            }
            // if user has given any extra content
            if (extraMsg != null && extraMsg.Length > 0)
            {
                // add the content
                content.Append("\n").Append(extraMsg);
            }
            bool retValue = false;
            // opens message window
            Message d = new Message(header, content.ToString(), Message.MessageType.QUESTION);
            d.Show();
            //if (d.DialogResult != null && (bool)d.DialogResult)// d.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            //{
            //    // if user clicks on OK button change the value
            //    retValue = true;
            //}
            return d;
        }

        /// <summary>
        /// Ask Question with question icon and (OK) (Cancel) buttons
        /// </summary>
        /// <param name="keyName">Keyword Name</param>
        /// <param name="isMsgText">if true returns Message Text, if false returns Message Tip
        ///     and if null then returns both message text and tip.</param>
        /// <param name="extraMsg">extra message to be displayed</param>
        /// <param name="header">header text</param>
        /// <returns>Bool type-true if user clicks OK button else false</returns>
        /// <author>Veena Pandey</author>
        public static bool Ask(string keyName, bool? isMsgText, string extraMsg, string header)
        {
            StringBuilder content = new StringBuilder();
            // if user has given a key
            if (keyName != null && !keyName.Equals(""))
            {
                // get key's value
                content.Append(Msg.GetMsg(Envs.GetContext(), keyName));
            }
            // if user has given any extra content
            if (extraMsg != null && extraMsg.Length > 0)
            {
                // add the content
                content.Append("\n").Append(extraMsg);
            }
            bool retValue = false;
            // opens message window
            Message d = new Message(header, content.ToString(), Message.MessageType.QUESTION);
            if (d.DialogResult != null && (bool)d.DialogResult)// d.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                // if user clicks on OK button change the value
                retValue = true;
            }
            return retValue;
        }

        /**************************************************************************
	 *	Ask Question with question icon and (OK) (Cancel) buttons
	 *	@param	WindowNo	Number of Window
	 *  @param  c           Container (owner)
	 *	@param	AD_Message	Message to be translated
	 *	@param	msg			Additional clear text message
	 *	@return true, if OK
	 */
        //    public static bool Ask(int WindowNo, System.Windows.Forms.Control c, String AD_Message, String msg)
        //{
        //    log.info(AD_Message + " - " + msg);
        //    Context ctx = Env.GetCtx();
        //    StringBuilder out1 = new StringBuilder();
        //    if (AD_Message != null && !AD_Message.Equals(""))
        //    {
        //        out1.Append(Msg.GetMsg(ctx, AD_Message));
        //    }
        //    if (msg != null && msg.Length > 0)
        //    {
        //        out1.Append("\n").Append(msg);
        //    }
        //    //
        //    System.Windows.Forms.Form parent =c.Parent;// Env.getParent(c);
        //    if (parent == null)
        //    {
        //        parent = Env.GetWindow(WindowNo);
        //    }

        //    bool retValue = false;
        //    if (showDialog && parent != null)
        //    {
        //        VAdvantage.Framework.Message d=new VAdvantage.Framework.Message(Env.getHeader(ctx, WindowNo),
        //        if (parent is JFrame)
        //        {
        //            ADialogDialog d = new ADialogDialog ((JFrame)parent,
        //                Env.getHeader(ctx, WindowNo),
        //                out1.toString(),
        //                JOptionPane.QUESTION_MESSAGE);
        //            retValue = d.getReturnCode() == ADialogDialog.A_OK;
        //        }
        //        else
        //        {
        //            ADialogDialog d = new ADialogDialog ((JDialog)parent,
        //                Env.getHeader(ctx, WindowNo),
        //                out.toString(),
        //                JOptionPane.QUESTION_MESSAGE);
        //            retValue = d.getReturnCode() == ADialogDialog.A_OK;
        //        }
        //    }
        //    else
        //    {
        //        Object[] optionsOC = {Util.cleanMnemonic(Msg.getMsg(ctx, "OK")), Util.cleanMnemonic(Msg.getMsg(ctx, "Cancel"))};
        //        int i = JOptionPane.showOptionDialog(parent,
        //            out.toString() + "\n",			//	message
        //            Env.getHeader(ctx, WindowNo),	//	title
        //            JOptionPane.DEFAULT_OPTION,
        //            JOptionPane.QUESTION_MESSAGE,
        //            null,
        //            optionsOC,
        //            optionsOC[0]);
        //        retValue = i == JOptionPane.YES_OPTION;
        //    }
        //    return retValue;
        //}
        /// <summary>
        /// Display error with error icon
        /// </summary>
        /// <param name="keyName">Keyword Name</param>
        /// <param name="isMsgText">if true returns Message Text, if false returns Message Tip
        ///     and if null then returns both message text and tip.</param>
        public static void Error(string keyName, bool? isMsgText)
        {
            Error(keyName, isMsgText, "", null);
        }

        /// <summary>
        /// Display error with error icon
        /// </summary>
        /// <param name="keyName">Keyword Name</param>
        /// <param name="isMsgText">if true returns Message Text, if false returns Message Tip
        ///     and if null then returns both message text and tip.</param>
        /// <param name="extraMsg"></param>
        /// <author>Veena Pandey</author>
        public static void Error(string keyName, bool? isMsgText, string extraMsg)
        {
            Error(keyName, isMsgText, extraMsg, null);
        }


        /// <summary>
        /// Display error with error icon
        /// </summary>
        /// <param name="keyName">Keyword Name</param>
        /// <param name="isMsgText">if true returns Message Text, if false returns Message Tip
        ///     and if null then returns both message text and tip.</param>
        /// <param name="extraMsg">extra message to be displayed</param>
        /// <param name="lblStatus"></param>
        /// <author>Veena Pandey</author>
        public static void Error(string keyName, bool? isMsgText, string extraMsg, Label lblStatus)//System.Windows.Forms.ToolStripLabel lblStatus)
        {
            StringBuilder content = new StringBuilder();
            // if user has given a key
            if (keyName != null && !keyName.Equals(""))
            {
                // get key's value
                content.Append(Msg.GetMsg(Envs.GetContext(), keyName));
            }
            // if user has given any extra content
            if (extraMsg != null && extraMsg.Length > 0)
            {
                // add the content
                content.Append("\n").Append(extraMsg);
            }
            // if user has given statusbar label, then show the messsage on status bar also
            if (lblStatus != null)
            {
                string statusText;
                if (isMsgText == null)
                {
                    // if user wants message test as well as message tooltip text, concatenate both
                    statusText = Msg.GetMsg(Envs.GetContext(), keyName, true) + "-" + Msg.GetMsg(Envs.GetContext(), keyName, false) + "-" + extraMsg;
                }
                else
                {
                    statusText = Msg.GetMsg(Envs.GetContext(), keyName, isMsgText) + "-" + extraMsg;
                }
                CommonFunctions.ShowErrorMessage(statusText, lblStatus);
            }
            //ErrorLog.FillErrorLog("CLASSSSSSSSSSS.MEFH", "errrrror query", "exxxxxxxxx", Message.MessageType.ERROR);
            // opens message window
            Message.ShowMessage("", content.ToString(), Message.MessageType.ERROR);
        }

        /// <summary>
        /// Display warning with warning icon
        /// </summary>
        /// <param name="keyName">Keyword Name</param>
        /// <param name="isMsgText">if true returns Message Text, if false returns Message Tip
        ///     and if null then returns both message text and tip.</param>
        /// <param name="extraMsg">extra message to be displayed</param>
        /// <param name="classMethod">of pattern "ClassName.MethodName"</param>
        /// <author>Veena Pandey</author>
        public static void Warn(string keyName, bool? isMsgText, string extraMsg, string classMethod)
        {
            Warn(keyName, isMsgText, extraMsg, classMethod, null);
        }

        /// <summary>
        /// Display warning with warning icon
        /// </summary>
        /// <param name="keyName">Keyword Name</param>
        /// <param name="isMsgText">if true returns Message Text, if false returns Message Tip
        ///     and if null then returns both message text and tip.</param>
        /// <param name="extraMsg">extra message to be displayed</param>
        /// <param name="classMethod">of pattern "ClassName.MethodName"</param>
        /// <param name="lblStatus"></param>
        /// <author>Veena Pandey</author>
        public static void Warn(string keyName, bool? isMsgText, string extraMsg, string classMethod, Label lblStatus)//System.Windows.Forms.ToolStripLabel lblStatus)
        {
            StringBuilder content = new StringBuilder();
            // if user has given a key
            if (keyName != null && !keyName.Equals(""))
            {
                // get key's value
                content.Append(Msg.GetMsg(Envs.GetContext(), keyName));
            }
            // if user has given any extra content
            if (extraMsg != null && extraMsg.Length > 0)
            {
                // add the content
                content.Append("\n").Append(extraMsg);
            }
            // if user has given statusbar label, then show the messsage on status bar also
            if (lblStatus != null)
            {
                string statusText;
                if (isMsgText == null)
                {
                    // if user wants message test as well as message tooltip text, concatenate both
                    statusText = Msg.GetMsg(Envs.GetContext(), keyName, true) + "-" + Msg.GetMsg(Envs.GetContext(), keyName, false) + "-" + extraMsg;
                }
                else
                {
                    statusText = Msg.GetMsg(Envs.GetContext(), keyName, isMsgText) + "-" + extraMsg;
                }
                CommonFunctions.ShowErrorMessage(statusText, lblStatus);
            }
            // if user has given class method name, then fill error log
            if (classMethod != null && classMethod.Trim() != "")
            {
                ErrorLog.FillErrorLog(classMethod, keyName + " - " + extraMsg, "", Message.MessageType.WARNING);
            }
            // opens message window
            Message.ShowMessage(Msg.GetMsg(Envs.GetCtx(), "Warning"), content.ToString(), Message.MessageType.WARNING);
        }

        /// <summary>
        /// Show message with info icon
        /// </summary>
        /// <param name="keyName">Keyword Name</param>
        /// <param name="isMsgText">if true returns Message Text, if false returns Message Tip
        ///     and if null then returns both message text and tip.</param>
        /// <param name="extraMsg">extra message to be displayed</param>
        /// <param name="classMethod">of pattern "ClassName.MethodName"</param>
        /// <author>Veena Pandey</author>
        public static void Info(string keyName, bool? isMsgText, string extraMsg, string classMethod)
        {
            Info(keyName, isMsgText, extraMsg, "", classMethod);
        }

        /// <summary>
        /// Show message with info icon
        /// </summary>
        /// <param name="keyName">Keyword Name</param>
        /// <param name="isMsgText">if true returns Message Text, if false returns Message Tip
        ///     and if null then returns both message text and tip.</param>
        /// <param name="extraMsg">extra message to be displayed</param>
        /// <param name="header">Header text</param>
        /// <param name="classMethod">of pattern "ClassName.MethodName"</param>
        /// <author>Veena Pandey</author>
        public static void Info(string keyName, bool? isMsgText, string extraMsg, string header, string classMethod)
        {
            StringBuilder content = new StringBuilder();
            // if user has given a key
            if (keyName != null && !keyName.Equals(""))
            {
                // get key's value
                content.Append(Msg.GetMsg(Envs.GetContext(), keyName));
            }
            // if user has given any extra content
            if (extraMsg != null && extraMsg.Length > 0)
            {
                // add the content
                content.Append("\n").Append(extraMsg);
            }
            // if user has given class method name, then fill error log
            if (classMethod != null && classMethod.Trim() != "")
            {
                ErrorLog.FillErrorLog(classMethod, keyName + "-" + extraMsg, "", Message.MessageType.INFORMATION);
            }
            if (content.ToString().Contains("\n"))
            {   
                //content.Replace("\n", " ");
            }
            if (content.ToString().Contains("\t"))
            {
                content.Replace("\t", " ");
            }
            // opens message window
            Message.ShowMessage(header, content.ToString(), Message.MessageType.INFORMATION);
        }

    }
}
